/* 
	@Copyright (c) 2007 by Denis Riabtchik. All rights reserved. See license.txt and http://jgrouse.com for details@
	$Id: DocElement.java 374 2008-03-08 20:47:55Z denis.riabtchik $
*/
package jgrouse.jgdoc.elements;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgrouse.jgdoc.JGDocException;
import jgrouse.jgdoc.api.IComment;
import jgrouse.jgdoc.api.IContainerElement;
import jgrouse.jgdoc.api.IDocElement;


public class DocElement implements IDocElement, Serializable
{

	private static final long serialVersionUID = 7367300407809068535L;
	protected IComment m_comment;
	private Map<String, String> m_attributes;

	private String m_fullName;
	private String m_localName;
	private IDocElement m_physOwner;
	private String m_parentName;
	private IContainerElement m_owner;

	public DocElement()
	{
		super();
		init();
	}
	
	public void setupReferences(Map<String, IDocElement> pNames)
	{
		
	}
	
	public void postProcess() throws JGDocException
	{
		
	}

	protected void init()
	{
		m_attributes = new HashMap<String, String>();
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getComment()
	 */
	public IComment getComment()
	{
		return m_comment;
	}

	public void addAttribute(String key, String value)
	{
		m_attributes.put(key, value);
	}
	
	protected String getText()
	{
		return "";
	}
	
	public String getCommentType()
	{
		return m_comment == null? "namespace" : m_comment.getCommentType().toString().toLowerCase();
	}
	
	public String getCapCommentType()
	{
		String str = getCommentType();
		return Character.toUpperCase(str.charAt(0)) + str.substring(1);
	}

	public String toString()
	{
		StringBuffer buf = new StringBuffer();
		if (m_physOwner != null)
		{
			addAttribute("physOwner", m_physOwner.getFullName());
		}
		String commentType = getCommentType();
		buf.append('<').append(commentType);
		for (Map.Entry<String, String> attr : m_attributes.entrySet())
		{
			buf.append(' ').append(attr.getKey()).append("=\"").append(attr.getValue()).append('\"');
		}
		buf.append('>');
		buf.append(getText());
		if (m_comment != null)
		{
			buf.append(m_comment.toString());
		}
		buf.append("</").append(commentType).append(">");
		
		
		return buf.toString();
	}

	public void setComment(IComment pComment)
	{
		m_comment = pComment;
		String elementType = m_comment.getTokenType().toString().toLowerCase();
		addAttribute("elementType", elementType);
	}
	
	public String getElementType()
	{
		return getAttribute("elementType");
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getFullName()
	 */
	public String getFullName()
	{
		return m_fullName;
	}

	public void setFullName(String pFullName)
	{
		m_fullName = pFullName;
		addAttribute("id", pFullName);
		String path = m_fullName.replace('.', '/');
		addAttribute("path", path);
	}
	
	public String getPath()
	{
		return getAttribute("path");
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getLocalName()
	 */
	public String getLocalName()
	{
		return m_localName;
	}

	public void setLocalName(String pLocalName)
	{
		m_localName = pLocalName;
		addAttribute("localName", pLocalName);
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getPhysOwner()
	 */
	public IDocElement getPhysOwner()
	{
		return m_physOwner;
	}

	public void setPhysOwner(IDocElement pPhysOwner)
	{
		m_physOwner = pPhysOwner;
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getParentName()
	 */
	public String getOriginalParentName()
	{
		return m_parentName;
	}
	
	public String getParentName()
	{
		return getAttribute("parentName");
	}

	public void setParentName(String pParentName)
	{
		m_parentName = pParentName;
		addAttribute("parentName", m_parentName == null || m_parentName.length() == 0? DocManager.GLOBAL_NAMESPACE : m_parentName);
	}

	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IDocElement#getOwner()
	 */
	public IContainerElement getOwner()
	{
		return m_owner;
	}

	public void setOwner(IContainerElement pOwner)
	{
		m_owner = pOwner;
	}

	public static String join(List<String> arr, String separator)
	{
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < arr.size(); i++)
		{
			if (i > 0)
			{
				buf.append(separator);
			}
			buf.append(arr.get(i));
		}
		return buf.toString();
	}
	
	public String getAttribute(String key)
	{
		return m_attributes.get(key);
	}

	public String getTimestamp()
	{
		return getAttribute("timestamp");
	}

	public void setTimestamp(String pTimestamp)
	{
		addAttribute("timestamp", pTimestamp);
	}

	public boolean isContainer()
	{
		return false;
	}
	
	public boolean hasSamePhysOwner()
	{
		return (m_owner.getPhysOwner() == getPhysOwner());
	}
	
	
}